#include "priority_queue.h"
#include "stack.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

void test_stack()
{
    stack<int, std::vector<int>> st;
    st.push(1);
    st.push(2);
    std::cout << st.top() << " ";
    st.push(3);
    st.push(4);
    while (!st.empty())
    {
        std::cout << st.top() << " ";
        st.pop();
    }
    std::cout << "\n";
}

template<class T, class Compare>
void BubbleSort(std::vector<T> a, int n, Compare com)
{
    int exchange = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (com(a[j], a[j + 1]))
            {
                exchange = 1;
                std::swap(a[j], a[j + 1]);
            }
        }
        if (exchange == 0)
            break;
    }
}

void test_priority_queue()
{
    // std::priority_queue<int> pq;
    // priority_queue<int, std::pmr::vector<int>, std::greater<int>> pq;
    priority_queue<int> pq;
    pq.push(4);
    pq.push(1);
    pq.push(6);
    pq.push(9);
    pq.push(4);
    while (!pq.empty())
    {
        std::cout << pq.top() << " ";
        pq.pop();
    }
    std::cout << "\n";

    std::vector<int> v = {3, 2, 4, 6, 1, 5};
    priority_queue<int> pq2(v.begin(), v.end());
    std::sort(v.begin(), v.end(), std::greater<int>());
    for (const auto &e : v)
        std::cout << e << " ";
    std::cout << "\n";

    v = {3, 2, 4, 6, 1, 5};
    BubbleSort(v, v.size(), Less<int>());
    for (const auto &e : v)
        std::cout << e << " ";
    std::cout << "\n";
    BubbleSort(v, v.size(), Greater<int>());
    for (const auto &e : v)
        std::cout << e << " ";
    std::cout << "\n";
}

struct OP1
{
    bool operator()(int x)
    {
        return x % 2 == 0;
    }
};

struct OP2
{
    int operator()(int x)
    {
        return x * 2;
    }
};

void test()
{
    // 查找第一个偶数
    int a[] = {3, 2, 4, 6, 1, 5};
    auto it = std::find_if(a, a + 6, OP1());
    std::cout << *it << std::endl;

    std::vector<int> v = {1, 2, 3, 4, 5, 6, 7};
    // 所有值乘2
    std::transform(v.begin(), v.end(), v.begin(), OP2());
    for (const auto &e : v)
        std::cout << e << " ";
    std::cout << "\n";
}

int main()
{
    // test_stack();
    // test_priority_queue();
    test();
    return 0;
}